using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Playables;

public class RroundAnimMovePlayableBehaviour : PlayableBehaviour
{
    public Transform player { get; set; }
    public Vector3 center { get; set; }
    public bool forward { get; set; }
    public float angleOffset { get; set; }
    protected Vector3 offset;
    protected float startRotate;
    protected float endRotate;
    protected float m_durection;
    protected float m_time;

    public override void ProcessFrame(Playable playable, FrameData info, object playerData)
    {
        m_time += info.deltaTime;
        var angle = Mathf.Lerp(startRotate, endRotate, m_time / m_durection);
        if (!forward)
            angle = -angle;
        var currentOffset = Quaternion.Euler(0,angle, 0) * offset;
        player.position = currentOffset + center;
        var eulerAngle = Quaternion.FromToRotation(Vector3.forward, -Vector3.Cross(Vector3.up, currentOffset)).eulerAngles;
        eulerAngle.x = eulerAngle.z = 0;
        eulerAngle.y += angleOffset;
        player.eulerAngles = eulerAngle;
    }

    public override void OnBehaviourPlay(Playable playable, FrameData info)
    {
        base.OnBehaviourPlay(playable, info);
        offset = player.transform.position - center;
        m_time = 0;
        m_durection = (float)playable.GetDuration();
        startRotate = 0;
        endRotate = 360;
    }

    public override void OnBehaviourPause(Playable playable, FrameData info)
    {
        base.OnBehaviourPause(playable, info);
    }
}